require("../css/register.less")
const CaptchaMini=require("../lib/captcha-mini")
document.ready(function(){
    /*不传值，统一走默认值*/
    let captcha1 = new CaptchaMini();
    let capt="";
    captcha1.draw(document.querySelector('#captcha1'), r => {
        // console.log(r, '验证码1');
        capt=r
    });
    // 获取元素
    let phoneInp=document.querySelector("#phoneInp")
    let captchaInp=document.querySelector("#captchaInp")
    let passwordInp=document.querySelector("#passwordInp")
    let passswordAgain=document.querySelector("#passswordAgain")
    let submitInp=document.querySelector("#submitInp")
    let hint=document.querySelector("#hint")
    submitInp.addEventListener("click",function(){
        let phone=phoneInp.value;
        let captcha=captchaInp.value;
        let password=passwordInp.value;
        let pwdAgain=passswordAgain.value;
        if(!window.$utils.isPhone(phone)){
            hint.textContent="您输入的账号有误"
        }else if (captcha.toLowerCase()!=capt.toLowerCase()) {
            hint.textContent="请重新输入验证码"
        }else if (!window.$utils.isPassword(password)) {
            hint.textContent="请输入正确的密码格式"
        }else if (password!=pwdAgain) {
            hint.textContent="两次密码输入不一致"
        }else{
            hint.textContent="";
            axios.post("http://139.9.177.51:8099/users/add",{
                "account":phone,
                "password":password
            })
            .then(function(res){
                let ret = res.data
                if (ret.status==0) {
                    window.$utils.toast("success", "注册成功")
                    setTimeout(function(){
                        location.href="./login.html"
                    },3000)
                }else{
                    window.$utils.toast("fail", "该账户已被注册")
                }
            })
            .catch(function(error){
                console.log(error);
            })
        }
    })
})